##LOAD R PACKAGES
library(corrplot)
library(ggcorrplot) 
source('utils.R')


##LODING CCES DATA
load('hh_cces_donors.rdata')
load('hh_cces_all.rdata')


cuse <- c("CC332A", "CC332B", "CC332C", "CC332D", "CC332E", "CC332F", "CC332G", "CC332H", "CC332I",
          "CC332J", "CC320", "CC322_1", "CC322_2", "CC322_3", "CC322_4", "CC322_5", "CC324",
          "CC325", "CC326", "CC327", "CC328", "CC329", "presvote", "CC322_6", "CC415r", "housevote",
          "CC302b", "CC422a", "CC422b")

###############################################################################
##CORR PLOTS
###############################################################################

##DONORS
rr.donors <- cces.donors[,cuse]
ccd <- cor(rr.donors[,'presvote'],rr.donors,use='complete.obs')
rr.donors[,ccd<0] <- -1 * rr.donors[,ccd<0] 
ccd <- cor(rr.donors[,'presvote'],rr.donors,use='complete.obs')
rr.donors <- rr.donors[,rev(order(ccd))]
colnames(rr.donors) <- pi.names[match(colnames(rr.donors),pi.names[,1]),2]
p.mat <- cor_pmat(rr.donors)
M <- cor(rr.donors,use='pairwise.complete.obs')
col <- colorRampPalette(c("white","white","#4477AA"))

##PDF
pdf(file='figures/fig_A3_corrplot_donors.pdf',width=15,height=15)
corrplot(M,
         method="color",
         col=col(300),  
         type="lower",
         number.cex=.72,number.digits=2,
         cl.pos='n',
         tl.cex=.75,
         number.font=2,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         diag=FALSE 
         )
dev.off()


##EPS
cairo_ps(file='figures/fig_A3_corrplot_donors.eps',width=15,height=15)
corrplot(M,
         method="color",
         col=col(300),  
         type="lower",
         number.cex=.72,number.digits=2,
         cl.pos='n',
         tl.cex=.75,
         number.font=2,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         diag=FALSE 
         )
dev.off()



##ALL RESPONDENTS
rr.all <- cces.all[,cuse]
ccd <- cor(rr.all[,'presvote'],rr.all,use='complete.obs')
rr.all[,ccd<0] <- -1 * rr.all[,ccd<0] 
ccd <- cor(rr.all[,'presvote'],rr.all,use='complete.obs')
rr.all <- rr.all[,rev(order(ccd))]
colnames(rr.all) <- pi.names[match(colnames(rr.all),pi.names[,1]),2]
p.mat <- cor_pmat(rr.all)
M <- cor(rr.all,use='pairwise.complete.obs')
col <- colorRampPalette(c("white","white","#4477AA"))

##PDF
pdf(file='figures/fig_A4_corrplot_nondonors.pdf',width=15,height=15)
corrplot(M,
         method="color",
         col=col(300),  
         type="lower",
         number.cex=.72,number.digits=2,
         cl.pos='n',
         tl.cex=.75,
         number.font=2,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         diag=FALSE 
         )
dev.off()


##EPS
cairo_ps(file='figures/fig_A3_corrplot_donors.eps',width=15,height=15)
corrplot(M,
         method="color",
         col=col(300),  
         type="lower",
         number.cex=.72,number.digits=2,
         cl.pos='n',
         tl.cex=.75,
         number.font=2,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         diag=FALSE 
         )
dev.off()


##ALL
## uu <- !is.na(cces$cfscoreR)
## rr <- cc2[!uu,] 
rr <- cces.all[,cuse]
ccd <- cor(rr[,'presvote'],rr,use='complete.obs')
rr[,ccd<0] <- -1 * rr[,ccd<0] 
ccd <- cor(rr[,'presvote'],rr,use='complete.obs')
rr <- rr[,rev(order(ccd))]
colnames(rr) <- pi.names[match(colnames(rr),pi.names[,1]),2]
p.mat <- cor_pmat(rr)
M <- cor(rr,use='pairwise.complete.obs')
col <- colorRampPalette(c("white","white","#4477AA"))

pdf(file='figures/fig_A3_corrplot_nondonors.pdf',width=15,height=15)
corrplot(M,
         method="color",
         col=col(300),  
         type="lower",
         number.cex=.72,number.digits=2,
         cl.pos='n',
         tl.cex=.75,
         number.font=2,
         ## order="hclust", 
         addCoef.col = "black", # Add coefficient of correlation
         tl.col="black", tl.srt=45, #Text label color and rotation
         ## tl.pos='r',
         ## tl.offset=2,
         # Combine with significance
         ##p.mat = p.mat, sig.level = 0.01, insig = "blank", 
         # hide correlation coefficient on the principal diagonal
         diag=FALSE 
         )
dev.off()
